package com.heima.media.controller;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.MaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.service.IWmMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 自媒体图文素材信息表 前端控制器
 * </p>
 *
 * @author SpaceTraveller
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/api/v1/material")
@Api(tags = "自媒体图文素材信息表接口")
public class WmMaterialController{

    @Autowired
    private IWmMaterialService wmMaterialService;

    @Autowired
    private MinIOService MinIOService;

    /**
     * 用户上传图片
     * @param file
     * @return
     */
    @PostMapping("/upload_picture")
    @ApiOperation(value = "用户上传图片",notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "file",value = "请求体对象",required = true,dataType = "MultipartFile")
    public ResponseResult<WmMaterial> uploadPic(MultipartFile file){
        User user = MediaThreadLocalUtil.get();
        if(user == null || StringUtils.isEmpty(user.getUserId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"请登录后上传");
        }
        String upload = MinIOService.upload(file);
        WmMaterial material = wmMaterialService.uploadPic(upload,user,0);
        return ResponseResult.okResult(material);
    }

    /**
     * 查看素材
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "查看素材",notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "dto",value = "请求体对象",required = true,dataType = "MaterialDto")
    public ResponseResult<List<WmMaterial>> listMaterial(@RequestBody MaterialDto dto){
        return wmMaterialService.listMaterial(dto);
    }

}
